package weq;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.dbcp.DbcpException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.By.ByCssSelector;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import comm.DbUtils;

public class WebXX {
	static WebDriver dr = null;
	private String gid;
	private String gname = null;
	private String myid = null;

	private String trueGid = null;

	private QueryRunner qr = null;
	private String pw = null;

	public WebXX(String trueGid, String gname, String myid, String pw) throws Exception {
		// System.setProperty("java.io.tmpdir", "z:\\temp");
		System.setProperty("webdriver.chrome.driver", "d:\\Chromium\\chromedriver.exe");
		
		//System.setProperty("webdriver.chrome.driver", "c:\\csuu\\chromedriver.exe");

		qr = DbUtils.getQueryRunner();

		dr = new ChromeDriver();

		this.trueGid = trueGid;
		this.gname = gname;
		this.myid = myid;
		this.pw = pw;

	}

	public void loadJQuery() throws IOException {
		executeScript(FileUtils.readFileToString(new File("wemscri/jq.js")));
	}

	public void sleep(long s) {
		try {
			Thread.sleep(s);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public WebElement waitWebElement(final By b) {
		return waitWebElement(b, 10);
	}

	public WebElement waitWebElement(final By b, int sec) {
		return new WebDriverWait(dr, sec).until(new ExpectedCondition<WebElement>() {
			@Override
			public WebElement apply(WebDriver dr) {
				return dr.findElement(b);
			}
		});
	}

	public Object executeScript(String js) {
		return ((JavascriptExecutor) dr).executeScript(js);
	}

	public String loadJS() throws IOException {
		return FileUtils.readFileToString(new File("wemscri/" + this.getClass().getSimpleName() + ".txt"));
	}

	private void say(String memid, String content) throws Exception {

		String memclic = "chatBox_groupMember_nick_" + gid + "_" + memid;
		dr.findElement(By.id(memclic)).click();

		sleep(5000);
		executeScript("$('#chatBox_inputBox_" + memid + " .rich_editor_div').html('" + content + "')  ");

		executeScript("$('.window_current').css('left','0')");
		executeScript("$('.window_current').css('top','0')");

		String sendid = "chatBox_sendMsgButton_" + memid;
		dr.findElement(By.id(sendid)).click();
		// executeScript("$('#"+sendid+"').click() ");

		sleep(5000);

		String closeid = "chatBox_closeButton_" + memid;
		dr.findElement(By.id(closeid)).click();
		// executeScript("$('#"+closeid+"').click() ");

		// insert into db
		qr.update("insert into qun(mid,gid) values ( ?,? ) ", memid, this.trueGid);

	}

	private void run() throws Exception {

		dr.get("http://w.qq.com/");
		loadJQuery();
		sleep(3000);

		waitWebElement(By.id("navbarHeaderImg")).click();
		waitWebElement(By.id("ifram_login"));
		dr.switchTo().frame("ifram_login");
		sleep(2000);
		waitWebElement(By.id("u")).click();
		sleep(2000);
		waitWebElement(By.id("u")).sendKeys(this.myid);
		waitWebElement(By.id("p")).sendKeys(this.pw);

		waitWebElement(By.id("login2qq_checkbox")).click();
		waitWebElement(By.id("login_button")).click();

		dr.switchTo().defaultContent();
		sleep(3000);

		waitWebElement(By.id("EQQ_TabGroupList")).click();
		sleep(2000);

		String js = " return   $(\".EQQ_GroupList_Name[title]:contains('" + gname + "')\").get(0) ";

		WebElement e = (WebElement) executeScript(js);
		String tmp = e.getAttribute("id");// EQQ_GroupList_Name_24216902

		this.gid = tmp.split("_")[3];

		List<String> done = qr.query("select mid from qun where gid = ?", new ColumnListHandler<String>("mid"), this.trueGid);
		done.add(myid);

		e.click();

		String mbclic = "chatBox_sideBar2_online_member_count_" + gid;
		waitWebElement(By.id(mbclic)).click();
		executeScript("$('.window_current').css('left','0')");
		executeScript("$('.window_current').css('top','0')");
		sleep(5000);

		String mbsel = "#chatBox_groupMember_" + gid + " .chatBox_groupMember_buddy";
		List<WebElement> mbs = dr.findElements(By.cssSelector(mbsel));

		executeScript("  $('#navbar').css('left','0')  ");

		// 3839717254
		// say("3839717254", System.currentTimeMillis() +
		// "良心品质，无效您退款     http://126.am/CNXgu0  "); // sleep(3000);

		// for (int i = mbs.size() - 1; i > -1; i--) {
		for (int i = 0; i < mbs.size(); i++) {
			WebElement memb = mbs.get(i);
			System.out.println(i);
			if (!memb.getAttribute("class").trim().equalsIgnoreCase("chatBox_groupMember_buddy")) {
				String memidStr = memb.getAttribute("id");
				String memid = memidStr.split("_")[3];
				if (!done.contains(memid)) {

					say(memid, System.currentTimeMillis() + "      http://t.cn/zTbkRc8   http://t.cn/zTbkxvx  <br> 如有打扰,请多多见谅 ^_^!"); // sleep(3000);

				} else {
					System.out.println("did");
				}
			}
		}

		dr.quit();
		System.exit(0);
	}

	public void close() {
		dr.quit();
	}

	public static void main(String[] args) {
		String trueGid = "242699558";
		String gname = "18岁的流年";
		String myid = "1754529864";
		String pw = "005006ppa";

		if (args.length == 4) {
			trueGid = args[0];
			gname = args[1];
			myid = args[2];
			pw = args[3];
		}

		while (true) {
			WebXX x = null;
			try {
				x = new WebXX(trueGid, gname, myid, pw);
				x.run();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				x.close();
			}
		}
	}

}
